{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68eaacbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入相关库\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import time\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from subprocess import check_output\n",
    "import lightgbm as lgb\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import confusion_matrix,log_loss\n",
    "from xgboost import XGBClassifier\n",
    "from lightgbm import LGBMClassifier \n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import cross_validate\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5cd1512f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取数据\n",
    "df_forest = pd.read_csv('./第一套题数据/data/train_forest_covertype.csv')\n",
    "\n",
    "#随机采样15120条数据\n",
    "# df_forest = df_forest.sample(n=15120)\n",
    "n = 15120\n",
    "df_forest_sample = df_forest.sample(n)\n",
    "df_forest_sample.head()\n",
    "#查看抽样数据\n",
    "df_forest_sample.describe()\n",
    "# 删除Soil_Type7、Soil_Type15 两列\n",
    "df_forest_sample.drop(axis=1,columns=['Soil_Type7','Soil_Type15'],inplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f549d3eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(15120, 54)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_forest_sample.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bd338bbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "XGB = XGBClassifier()\n",
    "lgbm = LGBMClassifier()\n",
    "\n",
    "first_models = [XGB,lgbm]   #存储的模型\n",
    "first_model_names = ['XGB','lgbm'] #模型名\n",
    "seed = 42\n",
    "skf = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "942d4780",
   "metadata": {},
   "source": [
    "1.ShuffleSplit函数对数据进行切分，指定参数plitting iterations为skf,test_size为0.3，train_size为0.6，random为seed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bcfd8fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import ShuffleSplit\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "#Define other steps\n",
    "n_folds = 5\n",
    "#由考生填写\n",
    "ShuffleSplit(n_splits=n_folds,test_size=0.3,train_size=0.6,random_state=seed)   # 切分数据\n",
    "std_sca = StandardScaler()  #标准化\n",
    "\n",
    "# 拆分数据\n",
    "X = df_forest_sample.drop(columns=['Cover_Type'],axis=1)\n",
    "y = pd.factorize(df_forest_sample['Cover_Type'])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a5dbe8e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "MLA_columns = ['MLA Name','MLA Parameters','MLA Train Accuracy Mean','MLA Test Accuracy Mean','MLA_time']\n",
    "MLA_compare = pd.DataFrame(columns=MLA_columns)\n",
    "#create table to compare MLA predictions\n",
    "MLA_predict = df_forest_sample[['Id']]\n",
    "train_size = X.shape[0]\n",
    "n_models = len(first_models)\n",
    "oof_pred = np.zeros((train_size,n_models))\n",
    "scores = []\n",
    "row_index = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9923f4c5",
   "metadata": {},
   "source": [
    "2.使用Pipeline进行模型训练set中指定标准为('Scaler',std_sca),模型为('Estimator',model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5420744a",
   "metadata": {},
   "source": [
    "3.使用cross_validate函数对模型进行评分，模型使用model,数据集使用X，y,指定return_train_score为True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80b67a4b",
   "metadata": {},
   "source": [
    "这里第2问和第三问得一起做，不像2.0分开做，这里做题的时候要注意这种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "07819a0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MLA Name</th>\n",
       "      <th>MLA_Parameters</th>\n",
       "      <th>MLA Train Accuracy Mean</th>\n",
       "      <th>MLA Test Accuracy Mean</th>\n",
       "      <th>MLA_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [MLA Name, MLA_Parameters, MLA Train Accuracy Mean, MLA Test Accuracy Mean, MLA_time]\n",
       "Index: []"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MLA_compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6674152b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001713 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2413\n",
      "[LightGBM] [Info] Number of data points in the train set: 12096, number of used features: 45\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001175 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2417\n",
      "[LightGBM] [Info] Number of data points in the train set: 12096, number of used features: 45\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001273 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2415\n",
      "[LightGBM] [Info] Number of data points in the train set: 12096, number of used features: 45\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001402 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2415\n",
      "[LightGBM] [Info] Number of data points in the train set: 12096, number of used features: 46\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001225 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2411\n",
      "[LightGBM] [Info] Number of data points in the train set: 12096, number of used features: 45\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.005185 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2404\n",
      "[LightGBM] [Info] Number of data points in the train set: 15120, number of used features: 46\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n",
      "[LightGBM] [Info] Start training from score -1.945910\n"
     ]
    }
   ],
   "source": [
    "for n,model in enumerate(first_models):\n",
    "    model_pipeline =  Pipeline(steps=[('Scaler',std_sca),('Estimator',model)])   #由考生填写\n",
    "    MLA_name = model.__class__.__name__\n",
    "    MLA_compare.loc[row_index,'MLA Name'] = MLA_name\n",
    "    MLA_compare.loc[row_index,'MLA Parameters'] = str(model.get_params())\n",
    "    \n",
    "    cv_results= cross_validate(estimator=model,X=X,y=y,cv=skf,return_train_score=True)     #由考生填写\n",
    "    MLA_compare.loc[row_index,'MLA Time'] = cv_results['fit_time'].mean()\n",
    "    MLA_compare.loc[row_index,'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()\n",
    "    MLA_compare.loc[row_index,'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()\n",
    "    model_pipeline.fit(X,y)\n",
    "    MLA_predict[MLA_name] = model_pipeline.predict(X)\n",
    "    row_index += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "019f14f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MLA Name</th>\n",
       "      <th>MLA Parameters</th>\n",
       "      <th>MLA Train Accuracy Mean</th>\n",
       "      <th>MLA Test Accuracy Mean</th>\n",
       "      <th>MLA_time</th>\n",
       "      <th>MLA Time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XGBClassifier</td>\n",
       "      <td>{'objective': 'multi:softprob', 'base_score': ...</td>\n",
       "      <td>0.996098</td>\n",
       "      <td>0.876058</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.899578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>LGBMClassifier</td>\n",
       "      <td>{'boosting_type': 'gbdt', 'class_weight': None...</td>\n",
       "      <td>0.992808</td>\n",
       "      <td>0.876124</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.589398</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         MLA Name                                     MLA Parameters  ... MLA_time  MLA Time\n",
       "0   XGBClassifier  {'objective': 'multi:softprob', 'base_score': ...  ...      NaN  2.899578\n",
       "1  LGBMClassifier  {'boosting_type': 'gbdt', 'class_weight': None...  ...      NaN  1.589398\n",
       "\n",
       "[2 rows x 6 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MLA_compare"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1478d434",
   "metadata": {},
   "source": [
    "4.使用sort_values对MLA_compare按照集MLA Test Accuracy Mean 这一列倒序排序，指定inplace为True覆盖原本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "30c0070c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([0, 1], dtype='int64')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MLA_compare.sort_values(by=['MLA Test Accuracy Mean'],ascending=True) #由考生填写\n",
    "MLA_compare\n",
    "MLA_compare.index[-20:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a95c68e3",
   "metadata": {},
   "source": [
    "5.删除MLA_compare后20位数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a526e2da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MLA Name</th>\n",
       "      <th>MLA Parameters</th>\n",
       "      <th>MLA Train Accuracy Mean</th>\n",
       "      <th>MLA Test Accuracy Mean</th>\n",
       "      <th>MLA_time</th>\n",
       "      <th>MLA Time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>LGBMClassifier</td>\n",
       "      <td>{'boosting_type': 'gbdt', 'class_weight': None...</td>\n",
       "      <td>0.992808</td>\n",
       "      <td>0.876124</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.589398</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         MLA Name                                     MLA Parameters  ... MLA_time  MLA Time\n",
       "1  LGBMClassifier  {'boosting_type': 'gbdt', 'class_weight': None...  ...      NaN  1.589398\n",
       "\n",
       "[1 rows x 6 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MLA_compare.drop(axis=0,index=MLA_compare.index[-20:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72022c68",
   "metadata": {},
   "outputs": [],
   "source": [
    "cro"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
